package freespiritsocial.io.user.ext

import android.app.Activity
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import freespiritsocial.io.user.R
import freespiritsocial.io.user.weight.MyDialog

/**
 * @author : hgj
 * @date : 2020/6/28
 */

//loading框
private var loadingDialog: MaterialDialog? = null

/**
 * 打开等待框
 */
fun AppCompatActivity.showLoadingExt(message: String = "Loading") {
    if (!this.isFinishing) {
        if (loadingDialog == null) {
            loadingDialog = MaterialDialog(this)
                .cancelable(true)
                .cancelOnTouchOutside(false)
                .cornerRadius(12f)
                .customView(R.layout.layout_custom_progress_dialog_view)
                .lifecycleOwner(this)
            loadingDialog?.getCustomView()?.run {
                this.findViewById<TextView>(R.id.loading_tips).text = message
            }
        }
        loadingDialog?.show()
    }
}


/**
 * 打开等待框
 */
fun Fragment.showLoadingExt(message: String = "Loading") {
    activity?.let {
        if (!it.isFinishing) {
            if (loadingDialog == null) {
                loadingDialog = MaterialDialog(it)
                    .cancelable(true)
                    .cancelOnTouchOutside(false)
                    .cornerRadius(12f)
                    .customView(R.layout.layout_custom_progress_dialog_view)
                    .lifecycleOwner(this)

                loadingDialog?.getCustomView()?.run {
                    this.findViewById<TextView>(R.id.loading_tips).text = message
                }
            }
            loadingDialog?.show()
        }
    }
}

/**
 * 打开对话框
 */
fun Fragment.showDialogWithEditViewExt(
    message: String,
    title: String,
    hint: String,
    negative: String = "Ok",
    positive: String = "",
    callback: ((String) -> Unit) = {}
) {
    activity?.let {
        if (!it.isFinishing) {
            MyDialog(
                it,
                title,
                message,
                hintText = hint,
                confirm = negative,
                cancel = positive,
                isOneBtn = positive.isEmpty(),
                callBack = callback
            ).show()
        }

    }
}

/**
 * 打开对话框
 */
fun Fragment.showDialogExt(
    message: String,
    title: String,
    negative: String = "Confirm",
    positive: String = "",
    callback: (() -> Unit) = {}
) {
    activity?.let {
        if (!it.isFinishing) {
            MyDialog(
                it,
                title,
                message,
                confirm = negative,
                cancel = positive,
                isOneBtn = positive.isEmpty(),
                callBack = { callback.invoke() }
            ).show()
        }

    }

}

/**
 * 打开对话框
 */
fun AppCompatActivity.showDialogExt(
    message: String,
    title: String,
    negative: String = "Confirm",
    positive: String = "",
    autoDismiss: Boolean = true,
    callback: (() -> Unit) = {}
) {
    this.let {
        if (!it.isFinishing) {
            MyDialog(
                it,
                title,
                message,
                confirm = negative,
                cancel = positive,
                isOneBtn = positive.isEmpty(),
                autoDismiss = autoDismiss,
                callBack = { callback.invoke() }
            ).show()
        }

    }

}

/**
 * 关闭等待框
 */
fun Activity.dismissLoadingExt() {
    loadingDialog?.dismiss()
    loadingDialog = null
}

/**
 * 关闭等待框
 */
fun Fragment.dismissLoadingExt() {
    loadingDialog?.dismiss()
    loadingDialog = null
}
